import { createStore } from 'vuex'
import createPersistedState from 'vuex-persistedstate'

import productModule from '@/store/modules/product'
import user from '@/store/modules/user'
import cart from '@/store/modules/cart'
import category from '@/store/modules/category'

export default createStore({
  state: {
    token: 'abc'
  },
  getters: {
    newToken(state) {
      return state.token + '!!!'
    }
  },
  mutations: {
    updateToken(state) {
      state.token = 'new token'
    }
  },
  actions: {
    updateToken(ctx) {
      // 发送请求
      setTimeout(() => {
        ctx.commit('updateToken')
      }, 1000)
    }
  },
  modules: {
    productModule,
    user,
    cart,
    category
  },
  plugins: [createPersistedState({
    key: 'erabbit-pc',
    paths: ['user', 'cart']
  })]
})
